var app = getApp()

Page({
  data: {
    autoplay: true,
    interval: 2000,
    duration: 500,
    circular: true,
    imgUrls: [
      // "/images/shouye_04.png",
      // "/images/shouye_05.png",
      // "/images/shouye_06.png"
    ],
    is_new_goods_hidden:false,
    is_hot_goods_hidden:false,
    new_goods:[

    ],
    items:[],
    street:[],
    hot_goods:[],

    key_word:"",

  },

  async init_no_userInfo(){
     console.log('游客登陆');
        
       var token='';
        my.getStorage({
          key: 'token', // 缓存数据的 key
          success: (res) => {
            console.log(res);
            token = res;
          },
        });
        if(token.length<8){
          this.add();
      this.getNewGoods();
        return;
        }else{
          token = await app.touristLogin(-1);
        }
      // console.log(j);
      app.globadData.access_token = token;
      app.globadData.isTourist = 'ture';
      
      //将游客token存储到本地
      my.setStorage({
        key: 'token', // 缓存数据的 key
        data: token, // 要缓存的数据
        success: (res) => {
          
        },
      });
      this.add();
      this.getNewGoods();
      this.getStreet();
  },


   async init(){
      var app = getApp()
      console.log('1');
      var i = await app.getUserInfo();
      // console.log('2');
      if(i==-1){
        console.log('拒绝授权！');
        
       var token='';
        my.getStorage({
          key: 'token', // 缓存数据的 key
          success: (res) => {
            console.log(res);
            token = res;
          },
        });
        if(token != '')
        token = await app.touristLogin(i);
      // console.log(j);
      app.globadData.access_token = token;
      app.globadData.isTourist = 'ture';
      
      //将游客token存储到本地
      my.setStorage({
        key: 'token', // 缓存数据的 key
        data: token, // 要缓存的数据
        success: (res) => {
          
        },
      });
      this.add();
      this.getNewGoods();
      this.getStreet();
        return;
      }
      console.log(i);
      console.log(i.data.auth_code);
      console.log('2');
      app.globadData.userInfo.nickName = i.data.nickName;
      console.log('3');
      app.globadData.userInfo.avatar = i.data.avatar;
      var j = await app.login(i.data.auth_code);
      console.log(j);
      app.globadData.access_token = j;
      this.add();
      this.getNewGoods();
      this.getStreet();
  },

  onLoad(query) {
    // debugger;
    // this.init();
      var app = getApp()
      console.log(app.globadData.access_token);
    // app.getUserInfo();
    
   
    //  my.confirm({
    //    title: '重庆鑫泰商城,是由重庆鑫泰科技有限责任公司经营的网上商城，所有商品均来自正规渠道，7天无理由退换货，敬请选购。为了更好的为您服务，我们将获取您的支付宝头像及昵称，如同意请点击确定，如不同意，请点击取消。', // confirm 框的标题
    //    success: (res) => {
    //      this.init();
    //    },
    //    fail:(res)=>{
         this.init_no_userInfo();
    //    }
    //  });
  },
  search_input:function(e){
     console.log(e.detail.value)
      this.setData({
      key_word: e.detail.value
    })
  },

  searchTab:function(e){

      
      if(this.data.key_word == '')
    {
         my.showToast({
           content: '请输入关键字哟', // 文字内容
           success: (res) => {
             
           },
          });
          return;
    }
      my.navigateTo({
      url: `/pages/main/goodsList/goodsList?search_word=${this.data.key_word}`, // 需要跳转的 tabBar 页面的路径（需在 app.json 的 tabBar 字段定义的页面），路径后不能带参数
    });


      
     
  },

    toDetail(e){
    var id = e.currentTarget.id;
    // console.log(e);
    my.navigateTo({
      url: `/pages/main/goods-detail/goods-detail?id=${id}`, // 需要跳转的 tabBar 页面的路径（需在 app.json 的 tabBar 字段定义的页面），路径后不能带参数
    });
  },

  /**
   * 获取新品列表
   */
  getNewGoods(){
      var img = this.new_goods;
      var app = getApp()
      // app.getUserInfo();
      // var url = app.globadData.apiUrlHeader+'get-goods-list'
      var url = app.globadData.apiUrlHeader+'get-mainpage-goods'
      my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify({
        'access_token':app.globadData.access_token
        }),
        dataType:'json',
        success: (res) => {
          console.log(res);
        if(res.data.status==0)
        {
          console.log(res);
           img = res.data.data;
          console.log(img);
          this.setData({
           items : img
          });
        }
      },
    });
  },

  getStreet() {
    var url = app.globadData.apiUrlHeader + 'get-stores-list';
    my.httpRequest({
      url: url, // 目标服务器 url
      method: 'POST',
      headers: {'Content-Type':'Application/json'},
      data: JSON.stringify({'access_token': app.globadData.access_token}),
      dataType: 'json',
      success: (res) => {
        console.log('street');
        this.setData({'street': res.data.data});
        console.log(res.data.data);
      },
    });
  },

  add(){
        var img = this.imgUrls;
        var app = getApp()
        
      console.log(app.globadData.access_token);   
        var url = app.globadData.apiUrlHeader+'get-ads';
        // var a_token = app.globadData.access_token;
        my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify({
         "access_token":app.globadData.access_token,
          "mini_program_id":"1",
          "ad_category_id":"1"
        }),
        dataType:'json',
        success: (res) => {
          console.log(res);
        if(res.data.status==0)
        {
          console.log(res.data.data);
           img = res.data.data;
          // console.log(img);
          this.setData({
           imgUrls : img
          });
        }
      },
      fail:(res)=>{
        console.log('in fail');
        console.log(res);
      },
    });
  },

   goListTap(){
     var query_name='';
     my.navigateTo({
      url: `/pages/main/goodsList/goodsList?search_word=${query_name}`, // 需要跳转的 tabBar 页面的路径（需在 app.json 的 tabBar 字段定义的页面），路径后不能带参数
    });
   },


  rediectCategory(e){
    console.log(e);
    var query_name = e.currentTarget.dataset.hi;
    console.log(query_name);
    my.navigateTo({
      url: `/pages/main/goodsList/goodsList?query_name=${query_name}`, // 需要跳转的 tabBar 页面的路径（需在 app.json 的 tabBar 字段定义的页面），路径后不能带参数
    });
  },
  redirectStoreList() {
    my.navigateTo({
      url: `/pages/main/store-list/store-list`,
    });
  },
});
